{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 4.1 \u624b\u52a8\u904d\u5386\u8fed\u4ee3\u5668\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u904d\u5386\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff0c\u4f46\u662f\u5374\u4e0d\u60f3\u4f7f\u7528for\u5faa\u73af\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u624b\u52a8\u7684\u904d\u5386\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u4f7f\u7528 next() \u51fd\u6570\u5e76\u5728\u4ee3\u7801\u4e2d\u6355\u83b7 StopIteration \u5f02\u5e38\u3002\n\u6bd4\u5982\uff0c\u4e0b\u9762\u7684\u4f8b\u5b50\u624b\u52a8\u8bfb\u53d6\u4e00\u4e2a\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u884c\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def manual_iter():\n    with open('/etc/passwd') as f:\n        try:\n            while True:\n                line = next(f)\n                print(line, end='')\n        except StopIteration:\n            pass"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u901a\u5e38\u6765\u8bb2\uff0c StopIteration \u7528\u6765\u6307\u793a\u8fed\u4ee3\u7684\u7ed3\u5c3e\u3002\n\u7136\u800c\uff0c\u5982\u679c\u4f60\u624b\u52a8\u4f7f\u7528\u4e0a\u9762\u6f14\u793a\u7684 next() \u51fd\u6570\u7684\u8bdd\uff0c\u4f60\u8fd8\u53ef\u4ee5\u901a\u8fc7\u8fd4\u56de\u4e00\u4e2a\u6307\u5b9a\u503c\u6765\u6807\u8bb0\u7ed3\u5c3e\uff0c\u6bd4\u5982 None \u3002\n\u4e0b\u9762\u662f\u793a\u4f8b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "with open('/etc/passwd') as f:\n    while True:\n        line = next(f, None)\n        if line is None:\n            break\n        print(line, end='')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4f1a\u4f7f\u7528 for \u5faa\u73af\u8bed\u53e5\u7528\u6765\u904d\u5386\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002\n\u4f46\u662f\uff0c\u5076\u5c14\u4e5f\u9700\u8981\u5bf9\u8fed\u4ee3\u505a\u66f4\u52a0\u7cbe\u786e\u7684\u63a7\u5236\uff0c\u8fd9\u65f6\u5019\u4e86\u89e3\u5e95\u5c42\u8fed\u4ee3\u673a\u5236\u5c31\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u7684\u4ea4\u4e92\u793a\u4f8b\u5411\u6211\u4eec\u6f14\u793a\u4e86\u8fed\u4ee3\u671f\u95f4\u6240\u53d1\u751f\u7684\u57fa\u672c\u7ec6\u8282\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "items = [1, 2, 3]\n# Get the iterator\nit = iter(items) # Invokes items.__iter__()\n# Run the iterator\nnext(it) # Invokes it.__next__()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "next(it)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "next(it)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "next(it)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u672c\u7ae0\u63a5\u4e0b\u6765\u51e0\u5c0f\u8282\u4f1a\u66f4\u6df1\u5165\u7684\u8bb2\u89e3\u8fed\u4ee3\u76f8\u5173\u6280\u672f\uff0c\u524d\u63d0\u662f\u4f60\u5148\u8981\u7406\u89e3\u57fa\u672c\u7684\u8fed\u4ee3\u534f\u8bae\u673a\u5236\u3002\n\u6240\u4ee5\u786e\u4fdd\u4f60\u5df2\u7ecf\u628a\u8fd9\u7ae0\u7684\u5185\u5bb9\u7262\u7262\u8bb0\u5728\u5fc3\u4e2d\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}